﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AustalSystem.NetWork
{
    /// <summary>
    /// winLog.xaml 的交互逻辑
    /// </summary>
    public partial class winLog : Window
    {
        public winLog()
        {
            InitializeComponent();      
         
            backGroundWorder.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(backGroundWorder_ProgressChanged);
            backGroundWorder.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(backGroundWorder_RunWorkerCompleted);
            backGroundWorder.DoWork += new System.ComponentModel.DoWorkEventHandler(backGroundWorder_DoWork);
            backGroundWorder.WorkerReportsProgress = true;
            backGroundWorder.WorkerSupportsCancellation = true;
            
        }

        void backGroundWorder_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {

            isGetConfirm = isCheckGetNetIden();
        }

        void backGroundWorder_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            this.btnResest.IsEnabled = true;
            this.btnCancal.IsEnabled = true;
            if (isGetConfirm)
            {
                this.backGroundWorder.Dispose();
                this.DialogResult = true;
                this.Close();

            }
            else
            {
                MessageBox.Show("注册信息验证失败，请重试！", "提示", MessageBoxButton.OK);
            }
        }

        void backGroundWorder_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            for (int i = 0; i < netWork.AuthenticationNetInfo.MessageReport.Count; i++)
            {
                this.lstLog.Items.Add(netWork.AuthenticationNetInfo.MessageReport[i]);
            }
            netWork.AuthenticationNetInfo.MessageReport.Clear();
        }

        private System.ComponentModel.BackgroundWorker backGroundWorder = new System.ComponentModel.BackgroundWorker();
        private  HANetwork.Network netWork=HANetwork.Network.DefaultNetWork;
        private bool isGetConfirm = false;

        public delegate void UpdateSoftWareFunc();

        AustalSystem.NetWork.winUpdateHelp winUpdata = new NetWork.winUpdateHelp();
        public void UpdateFunc()
        {
           

            netWork.UrlArg.DownLoadUrl.Add("http://www.eia65.com/index.php?option=com_product&lid=5&Itemid=28");//下载软件页面
            netWork.UrlArg.VersionUrl.Add("http://www.eia65.com/UserManage/WaterUpdater.xml");//版本网址

           

            //winUpdata.Owner = this;
            //winUpdata.Run();

            bool isNetWork = false;
            string mNetVersion = HANetwork.Network.DefaultNetWork.AuthenticationNetInfo.GetNetVersionNumber(HANetwork.Network.DefaultNetWork.UrlArg.VersionUrl, ref isNetWork);
            if (isNetWork)
            {

                string path = AppDomain.CurrentDomain.BaseDirectory + "HA.ModFlow.Presentation.exe";
                System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(path);
                System.Reflection.AssemblyName assemblyName = assembly.GetName();              

                string oldVersionStr =  assemblyName.Version.ToString();// HANetwork.Network.DefaultNetWork.AuthenticationNetInfo.GetComputerVersion();
                int oldVersion = HANetwork.Network.DefaultNetWork.AuthenticationNetInfo.ConvertVersion(oldVersionStr);
                int newVersion = HANetwork.Network.DefaultNetWork.AuthenticationNetInfo.ConvertVersion(mNetVersion);

                if (newVersion > oldVersion)
                {
                    string str = "本程序当前版本为:" + oldVersionStr + "\r\n";
                    str += "本程序已更新到 " + mNetVersion + " 版本,请到 http://www.eia65.com 下载新版本程序,是否进行下载更新!";
                    if (MessageBox.Show(str, "软件更新提示", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                    {
                        HANetwork.Network.DefaultNetWork.AuthenticationNetInfo.RunDownFilePage(HANetwork.Network.DefaultNetWork.UrlArg.DownLoadUrl[0]);                       
                    }
                }
            }
        }

     



        public delegate bool  CheckIdenfFunc( );
        public bool CheckDataFunc()
        {
            //使用ui元素    
            string filePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\User.txt";
            string strCode = netWork.AuthenticationNetInfo.GetComputerIdentifyingCode(filePath);
            backGroundWorder.WorkerReportsProgress = true;
            backGroundWorder.WorkerSupportsCancellation = true;
            backGroundWorder.ReportProgress(1);

            bool b = false;
            if (netWork.AuthenticationNetInfo.IsIdentifyingCodeCompara(netWork.AuthenticationNetInfo.NetIdentifyingCode, netWork.AuthenticationNetInfo.NetIdentifyingTime, netWork.AuthenticationNetInfo.ServiceTime, strCode))
            {
                b = true;
            }
            else
            {
                AustalSystem.NetWork.winVerification winVer = new NetWork.winVerification(netWork.AuthenticationNetInfo.NetIdentifyingCode, netWork.AuthenticationNetInfo.NetIdentifyingTime, netWork.AuthenticationNetInfo.ServiceTime);
                winVer.Owner = this;
                if (winVer.ShowDialog().Value)
                {
                    b = true;
                }
                else
                {
                    b = false;
                }
            }
            return b;
        }


        private bool isCheckGetNetIden()
        {


            bool b = false;
            bool isNetWork = false;
            netWork.AuthenticationNetInfo.MessageReport.Add("正在注册编译相关信息，请等待....");
            backGroundWorder.WorkerReportsProgress = true;
            backGroundWorder.WorkerSupportsCancellation = true;
            backGroundWorder.ReportProgress(1);
            //软件版本更新

            //AustalSystem.NetWork.winUpdateHelp winUpdata =new winUpdateHelp  as MessageBox();
            //MessageBox.Show("asdfasdfasdfaasdfa", "asda",  winUpdata);
            //System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new UpdateSoftWareFunc(UpdateFunc));
            UpdateFunc();



            backGroundWorder.WorkerReportsProgress = true;
            backGroundWorder.WorkerSupportsCancellation = true;
            backGroundWorder.ReportProgress(1);

            netWork.UrlArg.ServiceTimeUrl.Add("http://www.eia65.com/date.php");
            netWork.UrlArg.ServiceTimeUrl.Add("http://sjzwrj.vip4.eshimai.com/date.php");
            netWork.AuthenticationNetInfo.ServiceTime = netWork.AuthenticationNetInfo.GetNetDateTime(netWork.UrlArg.ServiceTimeUrl, ref isNetWork);
            backGroundWorder.WorkerReportsProgress = true;
            backGroundWorder.WorkerSupportsCancellation = true;
            backGroundWorder.ReportProgress(1);


            if (isNetWork)
            {
                netWork.UrlArg.UserListUrl.Add("http://www.eia65.com/UserManage/WaterUser.txt");
                netWork.UrlArg.UserListUrl.Add("http://www.era65.com/UserManage/WaterUser.txt");
                netWork.UrlArg.UserListUrl.Add("http://sjzwrj.vip4.eshimai.com/UserManage/WaterUser.txt");
                netWork.AuthenticationNetInfo.NetIdentifyingCode = netWork.AuthenticationNetInfo.GetNetIdentifyingCodeAndTime(netWork.UrlArg.UserListUrl, isNetWork);
                backGroundWorder.WorkerReportsProgress = true;
                backGroundWorder.WorkerSupportsCancellation = true;
                backGroundWorder.ReportProgress(1);

                b = (bool)System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new CheckIdenfFunc(CheckDataFunc));

            }
            return b;
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.btnResest.IsEnabled = false;
            this.btnCancal.IsEnabled = false;
            this.backGroundWorder.RunWorkerAsync();

        }

        private void btnResest_Click(object sender, RoutedEventArgs e)
        {
            this .btnResest.IsEnabled=false ;
            this.btnCancal.IsEnabled = false;
            if (this .backGroundWorder.IsBusy )
            {
                 MessageBox.Show("网络程序仍在连接，请稍后...","提示",MessageBoxButton.OK) ;
                return ;
            }
            else 
            {
                this .lstLog.Items .Clear ();
                this .backGroundWorder .RunWorkerAsync();
            }    
        }
    }
}
